package com.gqh.weather.controller;

import com.gqh.weather.domain.dto.*;
import com.gqh.weather.domain.entity.Question;
import com.gqh.weather.exception.Result;
import com.gqh.weather.service.QuestionService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 题目管理控制器
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/pingjia/zh/v1/xy_question")
@RequiredArgsConstructor
public class QuestionController {

    private final QuestionService questionService;

    /**
     * 创建题目
     *
     * @param request 创建请求
     * @return 操作结果
     */
    @PostMapping("/create")
    public Result<Void> create(@Valid QuestionCreateRequest request) {
        log.info("创建题目请求: {}", request);
        return questionService.createQuestion(request);
    }

    /**
     * 更新题目
     *
     * @param request 更新请求
     * @return 操作结果
     */
    @PostMapping("/update")
    public Result<Void> update(@Valid QuestionUpdateRequest request) {
        log.info("更新题目请求: ID={}", request.getId());
        return questionService.updateQuestion(request);
    }

    /**
     * 删除题目
     *
     * @param id 题目ID列表（逗号分隔或多个id参数）
     * @return 操作结果
     */
    @PostMapping("/delete")
    public Result<Void> delete(@RequestParam List<Integer> id) {
        log.info("删除题目请求: IDs={}", id);
        return questionService.deleteQuestions(id);
    }

    /**
     * 题目列表查询
     *
     * @param page       页码
     * @param count      每页数量
     * @param type       学科筛选
     * @param qtype      题型筛选
     * @param if_publish 发布状态筛选
     * @param keyword    关键字搜索
     * @return 分页结果
     */
    @GetMapping("/list")
    public Result<MyPage<QuestionListData>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer count,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Integer qtype,
            @RequestParam(required = false) Integer if_publish,
            @RequestParam(required = false) String keyword) {

        QuestionListRequest request = new QuestionListRequest();
        request.setPage(page);
        request.setCount(count);
        request.setType(type);
        request.setQtype(qtype);
        request.setIfPublish(if_publish);
        request.setKeyword(keyword);

        log.info("查询题目列表请求: page={}, count={}, type={}, qtype={}, if_publish={}, keyword={}",
                page, count, type, qtype, if_publish, keyword);
        return questionService.listQuestions(request);
    }

//    /**
//     * 题目随机
//     *
//     * @param size       随机数量
//     * @param type       学科筛选
//     * @param qtype      题型筛选
//     * @param if_publish 发布状态筛选
//     * @param difficulty 难度
//     * @return ids
//     */
//    @GetMapping("/random")
//    public Result<List<Integer>> list(
//            @RequestParam(required = false) Integer size,
//            @RequestParam(required = false) Integer type,
//            @RequestParam(required = false) Integer qtype,
//            @RequestParam(required = false) Integer if_publish,
//            @RequestParam(required = false) Integer difficulty) {
//
//        QuestionRandomRequest request = new QuestionRandomRequest();
//        request.setSize(size);
//        request.setType(type);
//        request.setQtype(qtype);
//        request.setIfPublish(if_publish);
//        request.setDifficulty(difficulty);
//
//        return questionService.random(request);
//    }

    /**
     * 根据ID列表查询题目
     *
     * @param ids 逗号分隔的ID字符串
     * @return 题目列表
     */
    @GetMapping("/ids_list")
    public Result<List<Question>> idsList(@RequestParam String ids) {
        log.info("根据ID列表查询题目请求: ids={}", ids);
        return questionService.getQuestionsByIds(ids);
    }

    /**
     * 查询题目详情
     *
     * @param id 题目ID
     * @return 题目详情
     */
    @GetMapping("/info")
    public Result<Question> info(@RequestParam @NotNull Integer id) {
        log.info("查询题目详情请求: id={}", id);
        return questionService.getQuestionInfo(id);
    }

    /**
     * 发布/取消发布题目
     *
     * @param request 发布请求
     * @return 操作结果
     */
    @PostMapping("/publish")
    public Result<Void> publish(@Valid PublishRequest request) {
        log.info("发布题目请求: id={}, flag={}", request.getIds(), request.getFlag());
        return questionService.publishQuestion(request.getIds(), request.getFlag());
    }
}
